﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace YouOweMe.BLL
{
    public static class DebtManager
    {
        public static DAL.DebtCollection GetDebts(long userID)
        {
  
            SubSonic.Query getQuery = new SubSonic.Query(DAL.Debt.Schema.TableName);

            getQuery.AddWhere(DAL.Debt.IndebtedColumn.ColumnName,
                SubSonic.Comparison.Equals, userID);
            getQuery.AddWhere(DAL.Debt.PaidColumn.ColumnName,
                SubSonic.Comparison.Equals, false);

            DAL.DebtCollection collection = new DAL.DebtCollection();
            collection.LoadAndCloseReader(getQuery.ExecuteReader());

            return collection;

        }

        public static DAL.DebtCollection GetDebts(long indebtedID, long lenderID)
        {
            if (lenderID == indebtedID)
                return null;

            SubSonic.Query getQuery = new SubSonic.Query(DAL.Debt.Schema.TableName);
            
            getQuery.AddWhere(DAL.Debt.IndebtedColumn.ColumnName,
                SubSonic.Comparison.Equals, indebtedID);
            
            getQuery.AddWhere(DAL.Debt.LenderColumn.ColumnName,
                SubSonic.Comparison.Equals, lenderID);
            
            // see all about youself
            //  Session.Add(Constants.SessionVariables.UserID, peopleID);if
            bool isCurrentUser = false;
            if (HttpContext.Current.Session[Constants.SessionVariables.UserID] != null)
            {
                long currentUserID = Convert.ToInt64(HttpContext.Current.Session[Constants.SessionVariables.UserID]);
                if (currentUserID == indebtedID
                    || currentUserID == lenderID)
                    isCurrentUser = true;
            }
            // current user
            if(!isCurrentUser)
                getQuery.AddWhere(DAL.Debt.PrivateXColumn.ColumnName,
                    SubSonic.Comparison.Equals, false);

            getQuery.AddWhere(DAL.Debt.PaidColumn.ColumnName,
                SubSonic.Comparison.Equals, false);

            DAL.DebtCollection collection = new DAL.DebtCollection();
            collection.LoadAndCloseReader(getQuery.ExecuteReader());
            return collection;

        }

        public static DAL.DebtCollection GetLends(long userID)
        {

                SubSonic.Query getQuery = new SubSonic.Query(DAL.Debt.Schema.TableName);

                getQuery.AddWhere(DAL.Debt.LenderColumn.ColumnName,
                    SubSonic.Comparison.Equals, userID);
                getQuery.AddWhere(DAL.Debt.PaidColumn.ColumnName,
                    SubSonic.Comparison.Equals, false);

                DAL.DebtCollection collection = new DAL.DebtCollection();
                collection.LoadAndCloseReader(getQuery.ExecuteReader());

                return collection;

        }

        public static double GetTotalOfLends(long userID)
        {
            double total = 0;
            foreach (DAL.Debt debt in GetLends(userID))
            {
                total += debt.Amount;
            }
            return total;
        }

        public static double GetTotalOfDebts(long userID)
        {
            double total = 0;
            foreach (DAL.Debt debt in GetDebts(userID))
            {
                total += debt.Amount;
            }
            return total;
        }

        public static void InsertOneDebt(long userID, long IndebtedID, string Comment,
            double Amount, bool Private)
        {

            //check data validity
            if (!RulesCenter.AmountValidator.IsAmountValid(Amount))
            {
                System.Diagnostics.Trace.WriteLine("Amount must be greater than zero");
                return;
            }

            DAL.Debt newDebt = new DAL.Debt();
            newDebt.Amount = Amount;
            newDebt.Comment = Comment;
            newDebt.Indebted = IndebtedID;
            newDebt.Lender = userID;
            newDebt.LoadDate = DateTime.Now;
            newDebt.Paid = false;
            newDebt.PrivateX = Private;
            newDebt.Save();

            return;
        }

        public static void CloseOneDebt(long Id)
        {
            DAL.Debt newDebt = new DAL.Debt(Id);
            if (newDebt.IsLoaded)
            {
                newDebt.Paid = true;
                newDebt.PaidDate = DateTime.Now;
                newDebt.Save();
            }
        }
    }
}
